#include "udp.h"
#include <ctype.h>
#include <sqlite3.h>

#define DATABASE_NAME "dictionary.db"

int callback(void *NotUsed,int argc,char **argv,char **ColName){
	char *buf = NotUsed;
	if(argc == 2){
		//把查询到的字符串复制给buf
		strncpy(buf,argv[1],strlen(argv[1])+1);
	}else{
		//如果失败就将字符串置空
		buf[0] = '\0';
	}
	buf[strlen(argv[1])] = '\n';
//	buf[strlen(argv[1])+1] = '\0';
	return 0;
}

void udp_main(const int fd,const struct sockaddr_in *addr){
	int ret = 0;
	int rc;
	struct sockaddr_in client_addr;
	socklen_t addrlen = sizeof(client_addr);
	sqlite3 *db;
	char buf[BUFSIZ] = {};
	char *sql_query,*errmsg;
	//打开数据库
	if((rc = sqlite3_open(DATABASE_NAME,&db))){
		printf("[%s:%d]无法打开数据库:{%s}\n",__FUNCTION__,__LINE__,sqlite3_errmsg(db));
		exit(0);
	}
	//循环处理客户端数据
	while(1){
		/*接收客户端数据*/
		do{
			ret = recvfrom(fd, buf, BUFSIZ, 0, (struct sockaddr *)&client_addr, &addrlen);
		}while(ret < 0 && errno == EINTR);
		if(ret < 0){
			ErrExit("recvfrom");
		}
		printf("[%s:%d]收到的数据：{%s}\n",__FUNCTION__,__LINE__,buf);
		/*提取需要翻译的单词*/
		for(ret=0; isalpha(buf[ret]) || buf[ret]==' '; ret++);
		buf[ret] = '\0';
		/*用SQL语句查询*/
		sql_query = sqlite3_mprintf("select *from dictionary where english like '%s'",buf);
		rc = sqlite3_exec(db, sql_query, callback, buf, &errmsg);
		if(rc != SQLITE_OK){
			sprintf(buf,"查询失败：%s\n",errmsg);
			printf("[%s:%d]%s",__FUNCTION__,__LINE__,buf);
			sendto(fd,buf,strlen(buf)+1,0,(struct sockaddr *)&client_addr,addrlen);
			sqlite3_free(errmsg);
			continue;
		}
		sqlite3_free(sql_query);
		printf("[%s:%d]查询结果：%s\n",__FUNCTION__,__LINE__,buf);
		sendto(fd,buf,strlen(buf)+1,0,(struct sockaddr *)&client_addr,addrlen);
	}
		/*关闭数据库,以及套接字*/
		sqlite3_close(db);
		close(fd);
	
}
